package cn.com.bob.messageboard.interceptor;


import org.springframework.stereotype.Component;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * @Author Bob
 * @Version：1.0
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Cookie[] cookies = request.getCookies();

        if (cookies != null) {
            for (Cookie cookie : cookies) {
                // Check for the desired cookie by its name
                if ("token".equals(cookie.getName())) {
                    String cookieValue = cookie.getValue();
                    // Use the retrieved cookie value in your logic
                    System.out.println("Cookie value: " + cookieValue);
                    return true; // Proceed with the request
                }
            }
        }

        // Redirect to login.html
        response.sendRedirect("/login.html");
        return false; // Stop further processing
    }


}
